using System;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Model.StateCommands;
using Headspring.Training.Core.Services;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace Headspring.Training.UnitTests.Model.StateCommands
{
	[TestFixture]
	public class CompleteToAssignedCommandTester : StateCommandBaseTester
	{
		protected override StateCommandBase GetStateCommand(WorkOrder order,
		                                                    Employee employee)
		{
			return new CompleteToAssignedCommand(order, employee);
		}

		[Test]
		public void ShouldBeValid()
		{
			var order = new WorkOrder {Status = WorkOrderStatus.Complete};
			var employee = new Employee();
			order.Creator = employee;

			var command = new CompleteToAssignedCommand(order, employee);
			Assert.That(command.IsValid(), Is.True);
		}

		[Test]
		public void ShouldNotBeValidInWrongStatus()
		{
			var order = new WorkOrder {Status = WorkOrderStatus.Draft};
			var employee = new Employee();
			order.Creator = employee;

			var command = new CompleteToAssignedCommand(order, employee);
			Assert.That(command.IsValid(), Is.False);
		}

		[Test]
		public void ShouldNotBeValidWithWrongEmployee()
		{
			var order = new WorkOrder {Status = WorkOrderStatus.Complete};
			var employee = new Employee();

			var command = new CompleteToAssignedCommand(order, employee);
			Assert.That(command.IsValid(), Is.False);
		}

		[Test]
		public void ShouldShowCorrectVerb()
		{
			var order = new WorkOrder {Status = WorkOrderStatus.Cancelled};
			var command = new CompleteToAssignedCommand(order, new Employee());

			Assert.AreEqual("Re-Assign", command.TransitionVerbPresentTense);
			Assert.AreEqual("Re-Assigned", command.TransitionVerbPastTense);
		}

		[Test]
		public void ShouldTransitionStateProperly()
		{
			if (WorkOrderStatus.Complete != null)
			{
				var order = new WorkOrder
				            	{Number = "123", Status = WorkOrderStatus.Complete};
				var employee = new Employee();
				order.Creator = employee;

				var mocks = new MockRepository();
				var commandVisitor = mocks.StrictMock<IStateCommandVisitor>();
				commandVisitor.SaveWorkOrder(order);
				commandVisitor.SendMessage("You have re-assigned work order 123");
				commandVisitor.EditWorkOrder(order);
				mocks.ReplayAll();

				var command = new CompleteToAssignedCommand(order, employee);
				command.Execute(commandVisitor);

				mocks.VerifyAll();
				Assert.That(order.Status, Is.EqualTo(WorkOrderStatus.Assigned));
			}
		}
	}
}